package com.longxi.tianjintianbao.ui.activity.visitor;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.presenter.visitor.VisitorListAtPresenter;
import com.longxi.tianjintianbao.ui.view.visitor.IVisitorListAtView;

import butterknife.BindView;

/**
 * Created by 70339 on 2018/5/4.
 * 代码无八阿哥
 * 阿弥陀佛
 */
public class VisitorListActivity extends BaseActivity<IVisitorListAtView, VisitorListAtPresenter> implements IVisitorListAtView {

    private final String TAG = "VisitorListActivity";

    @BindView(R.id.llNotVisiting)
    LinearLayout mLlNotVisiting;
    @BindView(R.id.tvNotVisiting)
    TextView mTvNotVisiting;
    @BindView(R.id.ivFilter)
    ImageView mIvFilter;
    @BindView(R.id.tvVisit)
    TextView mTvVisit;
    @BindView(R.id.tvGoneAway)
    TextView mTvGoneAway;
    @BindView(R.id.vp)
    ViewPager mViewPager;

    @BindView(R.id.etKeyword)
    EditText mEtKeyword;
    @BindView(R.id.tvCancle)
    TextView mTvCancle;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_visitor_list;
    }

    @Override
    public void initData() {
        super.initData();
        mPresenter.setBar();
        mPresenter.initViewPager();
    }

    @Override
    public void initListener() {
        super.initListener();
        mTvRight.setOnClickListener(v -> jumpToActivityForResult(SignVisitorActivity.class, AppConst.IntentRequstCode.ACTIVITY_SIGN_VISITOR));
        mLlNotVisiting.setOnClickListener(v -> mPresenter.onBarSwitch(0));
        mTvVisit.setOnClickListener(v -> mPresenter.onBarSwitch(1));
        mTvGoneAway.setOnClickListener(v -> mPresenter.onBarSwitch(2));
        mIvFilter.setOnClickListener(v -> mPresenter.popFilter());

        getEtKeyword().addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.length() > 0){
//                    imageViewLeft.setImageResource(pressImage);
//                    imageViewRight.setVisibility(View.VISIBLE);
                    mTvCancle.setVisibility(View.VISIBLE);
                }else{
//                    imageViewLeft.setImageResource(nomalImage);
//                    imageViewRight.setVisibility(View.GONE);
                    mTvCancle.setVisibility(View.GONE);
                }
            }
        });
        getTvCancle().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getEtKeyword().setText("");
                getEtKeyword().setFocusable(true);
                getEtKeyword().setFocusableInTouchMode(true);
                getEtKeyword().clearFocus();
                ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(VisitorListActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        });
    }

    @Override
    protected VisitorListAtPresenter createPresenter() {
        return new VisitorListAtPresenter(this);
    }

    @Override
    public LinearLayout getLlNotVisiting() {
        return mLlNotVisiting;
    }

    @Override
    public TextView getTvNotVisiting() {
        return mTvNotVisiting;
    }

    @Override
    public ImageView getIvFilter() {
        return mIvFilter;
    }

    @Override
    public TextView getTvVisit() {
        return mTvVisit;
    }

    @Override
    public TextView getTvGoneAway() {
        return mTvGoneAway;
    }

    @Override
    public ViewPager getViewPager() {
        return mViewPager;
    }

    @Override
    public EditText getEtKeyword() {
        return mEtKeyword;
    }

    @Override
    public TextView getTvCancle() {
        return mTvCancle;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(RESULT_OK != resultCode){
            return;
        }
        mPresenter.refreshFragmentUi();
    }

}
